package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sc.nft.dao.UserMarketValueDao;
import com.sc.nft.dao.UserRelevanceDao;
import com.sc.nft.dao.UserWorkRoomDao;
import com.sc.nft.dto.UserEcologyHatchRecordsDao;
import com.sc.nft.entity.UserWorkRoom;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.UserMarketValueService;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.sc.nft.dao.UserCityServerCentreDao;
import com.sc.nft.entity.UserCityServerCentre;
import com.sc.nft.mapper.UserRelevanceMapper;
import java.math.BigDecimal;
import java.util.List;
/**
 * 用户市值表服务接口实现
 *
 * @author zxy
 * @since 2023-07-21 00:27:15
 * @description 用户市值
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserMarketValueServiceImpl implements UserMarketValueService {
    private final UserMarketValueDao userMarketValueDao;
    private final UserRelevanceDao userRelevanceDao;
    private final UserCityServerCentreDao userCityServerCentreDao;


    @Override
    public void initMarketValue() {
        userMarketValueDao.truncate();
        userMarketValueDao.init();
    }

    @Override
    public BigDecimal getMarketValue( Long userId) {
        List<Long> expectUserId = Lists.newArrayList();//排除的用户列表
        UserCityServerCentre userCityServerCentre = userCityServerCentreDao.selectByUserId(userId);
        if(ObjectUtil.isNull(userCityServerCentre)){
            expectUserId.addAll(userRelevanceDao.getNearWorkRoom(userId));
        }
        expectUserId.addAll(userRelevanceDao.getNearCityServer(userId));

        BigDecimal sumMarketValue = userMarketValueDao.getMarketValue(userId,expectUserId);

        return sumMarketValue;
    }
    private final UserWorkRoomDao userWorkRoomDao;
    private final UserEcologyHatchRecordsDao userEcologyHatchRecordsDao;
    @Override
    public BigDecimal getEcologyMarketValue(Long userId) {
        Boolean exclude = Boolean.TRUE;
        UserCityServerCentre userCityServerCentre = userCityServerCentreDao.selectByUserId(userId);
        if (!BeanUtil.isEmpty(userCityServerCentre)) {
            exclude = Boolean.FALSE;
        }
        // 获取排除社区工作室和城市服务中心的用户
        List<Long> usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters = userWorkRoomDao.getUsersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters(userId, exclude);
        BigDecimal userIdsYesterdayMarketValue = BigDecimal.ZERO;
        BigDecimal userIdsSmelt_market_value = BigDecimal.ZERO;
        if (CollUtil.isNotEmpty(usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters)) {
            // 获取usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters的数据
            userIdsYesterdayMarketValue = userMarketValueDao.getUserIdsYesterdayMarketValue(usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters);
            userIdsSmelt_market_value = userEcologyHatchRecordsDao.getUserIdsSmeltMarketValue(usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters);
            if (userIdsSmelt_market_value==null) {
                userIdsSmelt_market_value=BigDecimal.ZERO;
            }
            if (userIdsYesterdayMarketValue==null) {
                userIdsYesterdayMarketValue=BigDecimal.ZERO;
            }
        }
        userIdsYesterdayMarketValue=userIdsYesterdayMarketValue.add(userIdsSmelt_market_value);
        return userIdsYesterdayMarketValue;
    }

    @Override
    public BigDecimal getMarketValueService(Long userId) {
        List<Long> expectUserId = Lists.newArrayList();//排除的用户列表
        UserCityServerCentre userCityServerCentre = userCityServerCentreDao.selectByUserId(userId);
        if(ObjectUtil.isNull(userCityServerCentre)){
            expectUserId.addAll(userRelevanceDao.getNearWorkRoom(userId));
        }
        BigDecimal sumMarketValue = userMarketValueDao.getMarketValue(userId,expectUserId);
        return sumMarketValue;
    }
}